﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TBlog.Web.Models;
using TBlog.Web.Service;
using Newtonsoft.Json;
using TBlog.Web.ViewModel;
using TBlog.Web.Common.PageHelper;
using System.Text;
namespace TBlog.Web.Controllers
{
    public class HomeController : Controller
    {
        private const char Minus = '-';
        //
        // GET: /Home/
        IArticleService _articleservice;
        public HomeController(IArticleService articleseervice)
        {
            _articleservice = articleseervice;
        }
        public ActionResult Test()
        {
            string inpu = "Alone-Await-async-microsoft-osArp";
            string spli = Spliter(inpu);
            string kjk = Restore(inpu);
            return View("View1");
        }
        public static string Spliter(string input)
        {
            StringBuilder builder = new StringBuilder();
            int index = 0;
            foreach (char str in input)
            {
                if (str >= 'A' && str <= 'Z')
                {
                    if (index > 0)
                    {
                        builder.Append(Minus);
                    }
                    builder.Append(Char.ToLower(str));
                }
                else if (str == Minus)
                {
                    builder.Append(Minus);
                    builder.Append(Minus);
                }
                else
                {
                    builder.Append(str);
                }
                index++;
            }

            return builder.ToString();
        }

        /// <summary>
        /// 把用 - 连接的小写单词还原为大写形式
        /// </summary>
        /// <param name="input"> </param>
        /// <returns> </returns>
        public static string Restore(string input)
        {
            StringBuilder builder = new StringBuilder();
            CharEnumerator iterator = input.GetEnumerator();
            int index = 0;
            while (iterator.MoveNext())
            {
                if (iterator.Current != Minus)
                {
                    char c = iterator.Current;
                    if (index == 0)
                    {
                        c = char.ToUpper(c);
                    }
                    builder.Append(c);
                    index++;
                    continue;
                }
                if (!iterator.MoveNext())
                {
                    builder.Append(Minus);
                    break;
                }
                if (iterator.Current == Minus)
                {
                    builder.Append(Minus);
                }
                else
                {
                    char c = iterator.Current;
                    c = char.ToUpper(c);
                    builder.Append(c);
                }
            }
            return builder.ToString();
        }
        public ActionResult Views()
        {
            return View();
        }
        public ActionResult Error()
        {
            return View();
        }
        public ActionResult Index()
        {
            var list = _articleservice.GetAll().OrderBy(o => o.ID).Skip(0).Take(10).ToList();
            return View(list);
        }
        /// <summary>
        /// 推荐页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Recommend()
        {
            return View();
        }
        [HttpGet]
        public JsonResult List(int pagesize = 10)
        {
            var page = Convert.ToInt32(Request["page"]) == 0 ? 1 : Convert.ToInt32(Request["page"]);
            var list = _articleservice.GetAll().OrderBy(o => o.ID).Skip(10 * (page - 1)).Take(pagesize).ToList();
            var datacount = _articleservice.GetAll().Count();
            var datapages = datacount % pagesize == 0 ? datacount / pagesize : datacount / pagesize + 1;
            var viewlist = new ViewArticleList() { Articlelist = list, DataPages = datapages };

            //  var data = list;
            // var list = _articleservice.GetArticleList(a => a.ID == a.ID, new Common.PageHelper.PageListRequest(1, 10, true));
            var json = JsonConvert.SerializeObject(viewlist);//"[{\"0\":\"1\"},{\"0\":\"1\"},{\"0\":\"5\"},{\"0\":\"7\"}]""{\"data\":\"" + json + "\",\"pages\":\"" + pages + "\"}"
            return Json(json, JsonRequestBehavior.AllowGet);
        }
        [HttpGet]
        public ActionResult Detail(int id)
        {
            //var id = Convert.ToInt32(Request["id"]);
            var model = _articleservice.GetByID(id);

            return View("Detail", model);
        }
        [HttpPost]
        public ActionResult Tags(string paramname)
        {
            var tag1 = Request["paramname"];
            var data = _articleservice.GetAll().Where(a => a.Tags.Contains(paramname)).ToList();
            var json = JsonConvert.SerializeObject(data);
            return Json(json);
        }

        [HttpPost]
        public ActionResult Category(int paramname)
        {
            //var cid = Convert.ToInt32(Request["cid"]);
            PagedList<Article> data = new PagedList<Article>();
            if (paramname == 0)
                data = _articleservice.GetAll().ToPagedList(a => a.ID == a.ID, new PageListRequest(1, 100, true));
            else
                data = _articleservice.GetAll().Where(a => a.CategoryID == paramname).ToPagedList(a => a.ID == a.ID, new PageListRequest(1, 100, true));

            var json = JsonConvert.SerializeObject(data);
            return Json(json);
        }
    }
}
